﻿package com.toncen.samepms.net.dao;

import java.util.List;

import com.toncen.samepms.common.AbstractDao;
import com.toncen.samepms.net.domain.Channel;
import com.toncen.samepms.net.domain.RoleChannel;
import com.toncen.samepms.system.domain.Role;

/**
 * 描述：角色栏目关系表NET_ROLE_CHANNEL的dao文件
 * @time 2013-5-10
 * @author xyp
 */
public class RoleChannelDao extends AbstractDao<RoleChannel> {

	public RoleChannelDao() {
	}

	/**
	 * 根据栏目id删除栏目
	 * @param channelId
	 * @throws Exception
	 */
	public void deleteByChannelId(long channelId) throws Exception {
		getSqlMap().delete(getStatement("deleteByChannelId"), channelId);
	}

	/**
	 * 根据角色id删除角色
	 * @param roleId
	 * @throws Exception
	 */
	public void deleteByRoleId(long roleId) throws Exception {
		getSqlMap().delete(getStatement("deleteByRoleId"), roleId);
	}

	/**
	 * 级联保存角色和栏目id
	 * @param roleId
	 * @param channelIds
	 * @throws Exception
	 */
	public void saveByRole(String roleId, String[] channelIds) throws Exception {
		deleteByRoleId(Long.parseLong(roleId));
		if (channelIds == null) {
			return;
		}
		RoleChannel roleChannel;
		Role role;
		Channel channel;
		for (String channelId : channelIds) { //循环栏目id
			roleChannel = new RoleChannel();
			role = new Role();
			roleChannel.setRole(role);
			role.setId(Long.parseLong(roleId));
			channel = new Channel();
			roleChannel.setChannel(channel);
			channel.setId(Long.parseLong(channelId));
			add(roleChannel);
		}
	}

	/**
	 * @param roleId
	 * @return
	 * @throws Exception
	 * 根据角色id查找相关角色
	 */
	public List<RoleChannel> getByRole(long roleId) throws Exception {
		return findList("getByRole", roleId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.db.BaseDao#initialize()
	 */
	@Override
	protected void initialize() {
		setStatementPrefix("RoleChannel");
		setStatementDivision(".");
	}
}
